﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ExportToRTR.CApExportToRTRStaticRail
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.ExportToRTR{

internal class CApExportToRTRStaticRail : CApExportToRTRDevice
{
  private ITxDevice m_rail;
  private List<TxRobot> m_robots;
  private List<ITxObject> m_jointsToExport;
  private List<ITxObject> m_childLinks;

  public CApExportToRTRStaticRail(ITxDevice rail, List<TxRobot> robots, string root)
    : base(rail, root, "")
  {
    this.m_rail = rail;
    this.m_robots = robots;
    this.m_directory = "cad";
    this.m_jointsToExport = this.GetJointsToExport();
    this.m_childLinks = new List<ITxObject>();
    this.m_jointsToExport.ForEach((Action<ITxObject>) (x => this.m_childLinks.Add((ITxObject) (x as TxJoint).ChildLink)));
  }

  private List<ITxObject> GetJointsToExport()
  {
    List<ITxObject> jointsToExport = new List<ITxObject>();
    foreach (ITxObject drivingJoint in (Collection<ITxObject>) this.m_rail.DrivingJoints)
    {
      foreach (TxRobot robot in this.m_robots)
      {
        if (((Collection<ITxObject>) robot.ExternalAxes).Contains(drivingJoint))
          jointsToExport.Add(drivingJoint);
      }
    }
    return jointsToExport;
  }

  public virtual void Export()
  {
    if (!Directory.Exists(Path.Combine(this.m_root, this.m_directory)))
      Directory.CreateDirectory(Path.Combine(this.m_root, this.m_directory));
    using (TxPoseData currentPose = this.m_rail.CurrentPose)
    {
      this.m_jointsToExport.ForEach((Action<ITxObject>) (x => (x as TxJoint).CurrentValue = 0.0));
      this.m_childLinks.ForEach((Action<ITxObject>) (x => ((IEnumerable<ITxObject>) (x as TxKinematicLink).LinkApprox()).ToList<ITxObject>().ForEach((Action<ITxObject>) (y => (y as ITxDisplayableObject).Blank()))));
      this.Export(this.m_rail as ITxLocatableObject);
      this.m_rail.CurrentPose = currentPose;
    }
  }
}
